<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dettaglio news</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../../css/main.css" rel="stylesheet" />
<link href="../../css/themes/default.css" rel="stylesheet" type="text/css"/>
<link href="../../css/themes/mac_os_x.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../js/prototype.js"></script>
<script type="text/javascript" src="../../js/scriptaculous.js"></script>
<script type="text/javascript" src="../../js/controls.js"></script>
<script type="text/javascript" src="../../js/page.js"></script>
<script type="text/javascript" src="../../js/window.js"></script>
<script type="text/javascript" src="../../js/window_effects.js"></script>
<script type="text/javascript">
var new_account = false;
var new_data = false;
var oldLink = null;
var _win;
var id_alunni = new Array;
<?php
if($_i != 0){
	//for($i = 0; $i < count($id_figli); $i++)
		//print("id_alunni[$i] = $id_figli[$i];\n");
	while(list($key, $value) = each($id_figli)){
		print("id_alunni['$key'] = '$value';\n");
	}
}
?>

function go(par, genit){
    if(par == 2){
        if(!confirm("Sei sicuro di voler cancellare questo utente?"))
            return false;
    }
    $('_i').value = genit;
    $('action').value = par;
    var url = "<?php print $_SESSION['__config__']['root_site'] ?>admin/adm_parents/parent_manager.php";
    //alert(url);
	var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: $('parent_form').serialize(true),
			    	onSuccess: function(transport){
			      		var response = transport.responseText || "no response text";
			      		//alert(response);
			      		var dati = response.split("|");
			      		if(dati[0] == "ko"){
							alert("Errore SQL. \nQuery: "+dati[1]+"\nErrore: "+dati[2]);
							return;
			      		}
			      		link = "genitori.php?msg="+par;
			      		if(par != 1){
							link += "&second=1&offset=<?php print $offset ?>";
			      		}

						parent.document.location.href = link;
			      		//parent.win.close();
			    	},
			    	onFailure: function(){ alert("Si e' verificato un errore...");}
			  });
    
}

function verifica(){
    //alert("ok");
	var nick = document.forms[0].uname.value;
	var url = "<?php print $_SESSION['__config__']['root_site'] ?>shared/verifica_login.php";
	var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: {nick: nick},
			    	onSuccess: function(transport){
			      		var response = transport.responseText || "no response text";
			      		var x = document.getElementById("check");
			            x.innerHTML = "";
			      		if(response == "ko"){
			      			x.style.color = "red";
			                x.innerHTML = "<br />Login gi&agrave; presente.";
			                return;
			     		}
			     		else{
			     			x.innerHTML = "<img src='../../images/check.png' style='width: 15px; height: 15px; vertical-align: bottom' />";
			            	new_account = true;
			            	document.getElementById("account_button").onclick = account_wrapper;
			     		}
			    	},
			    	onFailure: function(){ alert("Si e' verificato un errore...") }
			  });
}

function reg(par){
    var id = <?php print $_REQUEST['id'] ?>;
    if(par == 1){
		var nick = document.forms[0].uname.value;
		var pwd =  document.forms[0].pwd.value;
		var url = "<?php print $_SESSION['__config__']['root_site'] ?>admin/adm_parents/modifica_account.php";
		var req = new Ajax.Request(url,
				  {
				    	method:'post',
				    	parameters: {nick: nick, pwd: pwd},
				    	onSuccess: function(transport){
				      		var response = transport.responseText || "no response text";
				      		if(response == "ko"){
				      			alert(response);
				                return;
				     		}
				     		else{
				     			new_account = false;
								document.getElementById("account_button").onclick = no_change;
								var field = document.getElementById("account_field");
								var legend = document.getElementById("account_legend");
								field.style.border = "1px solid ";
								field.style.color = "#000000";
								alert("Account modificato correttamente");
				     		}
				    	},
				    	onFailure: function(){ alert("Si e' verificato un errore...") }
				  });
	}
}

function gen_pwd(){
	var pass = genera_password(document.forms[0].nome.value, document.forms[0].cognome.value);
	document.forms[0].pwd.value = pass;
	new_account = true;
	document.getElementById("account_button").setAttribute("onclick", "account_wrapper()");
	//alert(document.getElementById("account_button").onclick);
	var field = document.getElementById("account_field");
	var legend = document.getElementById("account_legend");
	field.style.border = "1px solid #ff0000";
	field.style.color = "#ff0000";
}

function gen_login(){
	if((trim(document.forms[0].nome.value) == "") || (trim(document.forms[0].cognome.value) == "")){
		alert("inserisci nome e cognome per generare la username");
		return;
	}
	var nome = document.forms[0].nome.value;
	var cognome = document.forms[0].cognome.value;
	var url = "<?php print $_SESSION['__config__']['root_site'] ?>admin/adm_parents/crea_login.php";
	var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: {nome: nome, cognome: cognome},
			    	onSuccess: function(transport){
			      		var response = transport.responseText || "no response text";
			      		var dati = response.split(":");
			      		if(dati[0] == "ko"){
			      			alert(dati[1]);
			                return;
			     		}
			     		else{
			     			document.forms[0].us.value = response;
			     		}
			    	},
			    	onFailure: function(){ alert("Si e' verificato un errore...") }
			  });
	
}

function no_change(){
	alert("Nessun dato modificato");
}

function account_wrapper(){
	reg(1);
}

function add_student(){
	/*
	funzione che gestisce i referenti, chiamando con AJAX il db se nuovo = 1
	e cancellando invece il contenuto della textarea se nuovo = 0
	*/
	parent._win.show();
}

function del(id){
	//alert("Chiamata a del("+id+")");
	var campo = "al"+id;
	//document.getElementById(campo).style.display = "none";
	//deleteArrayElement(id, id_alunni);
	delete id_alunni[id];
	//alert(document.getElementById("figli").innerHTML);
	var stringa_nomi = document.getElementById("figli").innerHTML;
	stringa_nomi = "";
	stringa_nomi = crea_stringa_nomi(id_alunni);
	document.getElementById("figli").innerHTML = stringa_nomi;
	document.forms[0].id_figli.value = crea_stringa_id(id_alunni);
}

function deleteArrayElement(elem, myarray){
	var c = myarray.length;
   	for(i = 0; i < c; i++){
   		if(myarray[i] == elem){
      		//alert("JS: Ho trovato "+elem);
			for(x = i+1; x < c; x++){
         		myarray[i++] = myarray[x];
         	}
         	myarray.pop();   
   		}
	}
}

function crea_stringa_nomi(ar){
	str = "";
	for(chiave in ar){
		if(!isNaN(chiave)){
			str += "<a href='#' onclick='del("+chiave+")' id='al"+chiave+"'>"+ar[chiave]+"</a>, ";
			alert("ar["+chiave+"]="+ar[chiave]);
		}
	}
	if(str.endsWith(", "))
		return str.substr(0, (str.length - 3)); 
	//alert(str);
	return str;
}

function crea_stringa_id(ar){
	var _a = new Array;
	for(chiave in ar){
		if(!isNaN(chiave))
			_a.push(chiave);
	}	
	return _a.join();
}

</script>
</head>
<body <?php if(isset($msg)) print("onLoad=\"alert('".$msg."'); window.close(); window.opener.document.location.href='genitori.php'\"") ?>>
    <p class="popup_header">Gestione dati genitore</p>
    <form action="dettaglio_genitore.php?upd=1" method="post" id="parent_form">
    <fieldset id="account_field" style="width: 550px; border: 1px solid; padding-top: 10px; margin-left: auto; margin-right: auto">
    <legend id="account_legend" style="font-weight: bold;">Account</legend>
    <table style="width: 530px">
        <tr class="popup_row header_row">
            <td class="popup_title">UserName</td>
            <td style="width: 250px">
                <input id="us" type="text" class="form_input" name="uname" style="width: 200px" value="<?php if(isset($genitore)) print($genitore['username']) ?>" <?php if($_i == 0) print("readonly='readonly'")  ?> />
                <span id="check"></span>
            </td>
            <td style="width: 130px">
                <?php if($_i != 0){ ?>
                <a href="#" onclick="verifica()">Verifica username</a>
                <?php 
				}else{
                ?>
                <a href="#" onclick="gen_login()">Crea username</a>
                <?php } ?>
                <span id="verify"></span>
            </td>
        </tr>
        <tr class="popup_row">
            <td class="popup_title">Password</td>
            <td style="width: 250px">
                <input type="password" class="form_input" name="pwd" style="width: 200px" value="" readonly="readonly" />
            </td>
            <td style="width: 130px">
              <a href="#" onclick="gen_pwd()">Genera password</a>
            </td>
        </tr>
        <tr class="popup_row">
            <td colspan="2" style="height: 5px"></td>
            <td>
            	<?php if($_i != 0){ ?>
            	<a href="#" id="account_button" onclick="alert('Nessun dato modificato')">Registra account</a>
            	<?php } ?>
            </td>
        </tr>
    </table>
    </fieldset>
    <fieldset style="width: 550px; border: 1px solid; padding-top: 10px; margin-top: 30px; margin-left: auto; margin-right: auto">
    <legend style="font-weight: bold">Dati personali</legend>
    <table width="530">
        <tr class="popup_row header_row">
            <td class="popup_title">Nome</td>
            <td colspan="2" style="width: 380px">
                <input type="text" name="nome" class="form_input" style="width: 380px" value="<?php if(isset($genitore)) print($genitore['nome']) ?>" />
            </td>
        </tr>
        <tr class="popup_row">
            <td class="popup_title">Cognome</td>
            <td colspan="2" style="width: 380px">
                <input type="text" name="cognome" class="form_input" style="width: 380px" value="<?php if(isset($genitore)) print($genitore['cognome']) ?>" />
            </td>
        </tr>
    	<tr class="popup_row">
            <td class="popup_title">Alunni</td>
            <td style="width: 250px">
                <span id="figli" style=""><?php print $figli ?></span>
                <input type="hidden" name="action" id="action" />
    			<input type="hidden" name="_i" id="_i" />
    			<input type="hidden" name="alunni" id="alunni" value="<?php print $a ?>" />
            </td>
            <td style="text-align: right"><a href="#" onclick="add_student()">Aggiungi</a></td>
        </tr>
        <tr class="popup_row">
            <td colspan="3" style="height: 5px"><input type="hidden" name="id_figli" value="<?php if(count($id_figli) > 0){ print join(",", array_keys($id_figli));} ?>" /></td>
        </tr>
        
    </table>
    </fieldset>
     
    <div style="width: 650px;  margin-top: 50px; padding-bottom: 60px; text-align: right">
        <a href="#" onclick="go(<?php if(isset($_REQUEST['id']) && $_REQUEST['id'] != 0) print("3, ".$_REQUEST['id']); else print("1, 0"); ?>)">Registra</a>&nbsp;&nbsp;|&nbsp;&nbsp;
        <?php if(isset($_REQUEST['id']) && $_REQUEST['id'] != 0){
        ?>
        <a href="#" onclick="go(2, <?php print $_REQUEST['id'] ?>)">Cancella genitore</a>&nbsp;&nbsp;|&nbsp;&nbsp;
        <?php
        }
        ?>
        <a href="#" onclick="parent.win.close()" style="margin-right: 55px;">Chiudi</a>
    </div>
   </form>
</body>
</html>